<template>
  <div>
    <el-card shadow="always" class="box-card margin_30">
      <div slot="header">
        <span>
          <el-button type="primary" size="mini" @click="isShow=true,isAdd=true">新增角色</el-button>
        </span>
      </div>
      <div>
        <el-table
          :data="roleList"
          border
          style="width: 100%"
        >
          <el-table-column
            type="index"
            label="#"
            fixed="left"
            width="50"
            align="center"
          />
          <el-table-column
            prop="roleName"
            label="角色名称"
            align="center"
            width="250"
          />
          <el-table-column
            prop="roleCode"
            label="角色编码"
            align="center"
            width="250"
          />

          <el-table-column
            prop="remark"
            label="备注"
            align="center"
          />

          <el-table-column
            prop="createTime"
            label="添加时间"
            align="center"
            width="300"
          />

          <el-table-column
            prop=""
            label="操作"
            fixed="right"
            align="center"
            width="150"
          >
            <template v-slot="scope">
              <el-button type="primary" size="small" @click="upPermissions(scope.row.id)">编辑</el-button>
              <el-button type="primary" size="small" @click="delPermissions(scope.row.id)">删除</el-button>
            </template>

          </el-table-column>
        </el-table>
        <el-pagination
          :current-page="start"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <el-dialog
      title="编辑角色"
      :visible.sync="isShow"
      width="40%"
    >
      <el-form :model="form" :inline="true" label-width="80px" size="mini">
        <el-form-item label="角色名称">
          <el-input v-model="form.roleName" />
        </el-form-item>
        <el-form-item label="角色编码">
          <el-input v-model="form.roleCode" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="form.remark" />
        </el-form-item>

        <el-row :gutter="20">
          <el-col :span="12" :offset="0">
            <el-form-item label="选择菜单">
              <div>
                <el-checkbox v-model="isOpen" label="" :indeterminate="false" @change="openTree">展开/关闭</el-checkbox>
                <el-checkbox v-model="isSelected" label="" :indeterminate="false" @change="selectTree">全选/反选</el-checkbox>

              </div>
              <el-tree
                ref="tree"
                :data="permissions"
                node-key="id"
                show-checkbox
                :props="props"
                @check="treeEvents"
                @check-change="handleCheckChange"
              />

            </el-form-item>
          </el-col>
        </el-row>

      </el-form>

      <span slot="footer">
        <el-button @click=" isShow = false">取消</el-button>
        <el-button type="primary" @click="addpermissions">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import mixins from '@/mixins/index'
import { saveRolePermissions, findAllPermissions, findRolePermissions, updateRolePermissions, removeRole } from '@/api/rightsManage/role/index'
import { findAllRoles } from '@/api/rightsManage/account/index'
export default {

  name: 'VueAdminTemplateMasterIndex',
  mixins: [mixins],
  data() {
    return {
      isOpen: false,
      isSelected: false,
      isAdd: '',
      ids: [],
      isShow: false,
      roleList: [],
      permissions: [],
      props: {
        label: 'title',
        children: 'children'
      },
      form: {
        'createTime': '',
        'id': '',
        'isDeleted': '',
        'modifyTime': '',
        'permissionIds': [],
        'remark': '',
        'roleCode': '',
        'roleName': ''
      },
      roleInfo: {}
    }
  },

  mounted() {

  },

  created() {
    this.init()
  },

  methods: {
    delPermissions(id) {
      removeRole(id).then(res => {
        this.init()
      })
    },
    upPermissions(id) {
      findRolePermissions(id).then(res => {
        this.isShow = true
        this.isAdd = false
        this.form = res.data.role
        this.$nextTick(() => {
          this.$refs.tree.setCheckedKeys(res.data.role.permissionIds)
          if (res.data.role.permissionIds.length == this.ids.length) {
            this.isSelected = true
          } else {
            this.isSelected = false
          }
        })
      })
    },
    addpermissions() {
      if (this.isAdd) {
        saveRolePermissions(this.form).then(res => {
          this.isShow = false
          this.init()
        })
      } else {
        updateRolePermissions(this.form).then(res => {
          this.isShow = false
          this.init()
        })
      }
    },
    openTree() {
      var obj = this.$refs.tree.store.nodesMap
      for (var i in obj) {
        obj[i].expanded = this.isOpen
      }
    },
    treeEvents(val, val1) {
      console.log(val, val1)
      if (val1.checkedKeys.length == this.ids.length) {
        this.isSelected = true
      } else {
        this.isSelected = false
      }
      this.form.permissionIds = val1.checkedKeys
    },
    filterId(arr, filterArr) {
      arr.forEach(ele => {
        filterArr.push(ele.id)
        if (ele.children && ele.children.length) {
          this.filterId(ele.children, filterArr)
        }
      })
    },
    selectTree() {
      if (this.isSelected) {
        this.$refs.tree.setCheckedKeys(this.ids)
        this.form.permissionIds = this.ids
        console.log(this.form)
      } else {
        this.$refs.tree.setCheckedKeys([])
        this.form.permissionIds = []
      }
    },
    handleCheckChange(obj, selected, childrenSelected) {
      console.log(obj, selected, childrenSelected)
    },
    init() {
      findAllPermissions().then(res => {
        this.filterId(res.data.menus, this.ids)
        this.permissions = res.data.menus
      })
      findAllRoles().then(res => {
        this.total = res.data.items.length
        this.roleList = res.data.items
      })
    }
  }
}
</script>

<style scoped>

</style>
